<template>
    <div class="upload-box">
        <div>
            <a-upload action="/" :show-file-list="false" :custom-request="front">
                <template #upload-button>
                    <div class="upload-ele" v-if="!frontUrl">
                        <icon-plus />
                        点击上传
                    </div>
                    <div v-else class="upload-img-preview">
                        <img :src="frontUrl" />
                    </div>
                </template>
            </a-upload>
            身份证正面照
        </div>
        <div>
            <a-upload action="/" :show-file-list="false" :custom-request="back">
                <template #upload-button>
                    <div class="upload-ele" v-if="!backUrl">
                        <icon-plus />
                        点击上传
                    </div>
                    <div v-else class="upload-img-preview">
                        <img :src="backUrl" />
                    </div>
                </template>
            </a-upload>
            身份证反面照
        </div>
    </div>
</template>

<script setup lang="ts">
import upload, { isLegitimate } from "../upload";
import { Message } from '@arco-design/web-vue';
import { compressorImage } from "@/utils/compressorFn";

defineProps({
    frontUrl: {
        type: String,
        required: true
    },
    backUrl: {
        type: String,
        required: true
    }
})
const emit = defineEmits(['update:frontUrl', 'update:backUrl']);
/**
 * 校验图片格式以及大小
 */
const validateImage = (file:File) => {
    const type = ['image/jpg', 'image/jpeg', 'image/png'];
    const legitimate:any = isLegitimate(file, type, 500);
    if (legitimate.msg) {
        Message.error(legitimate.msg);
        return false;
    } else {
        return true;
    }
}


/**
 * 身份证正面上传
 */
const front = async ({fileItem}:any) => {
  compressorImage(fileItem, () => {
    if (validateImage(fileItem.file)) {
      upload(fileItem.file).then(res => {
        emit("update:frontUrl", res.url);
      });
    }
  });
}
/**
 * 身份证反面上传
 */
const back = async ({fileItem}:any) => {
  compressorImage(fileItem, () => {
    if (validateImage(fileItem.file)) {
      upload(fileItem.file).then(res => {
        emit("update:backUrl", res.url);
      });
    }
  });
}
</script>

<style lang="scss" scoped>
.upload-box {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 30px;
    flex: 1;

    >div {
        width: 100%;
        text-align: center;
        color: #3D3D3D;
    }

    .upload-ele {
        background: #F2F3F5;
        height: 144px;
        margin-bottom: 10px;
        padding-top: 40px;
        box-sizing: border-box;
    }

    .upload-img-preview {
        height: 144px;

        img {
            height: 100%;
            width: 100%;
        }
    }

    ::v-deep(.arco-upload) {
        display: block;
        color: #C9CDD4;
        text-align: center;

        .arco-icon {
            display: block;
            margin: 0 auto 10px;
            font-size: 30px;
        }
    }
}
</style>